import parser from "@babel/parser"
import fs from "fs"
import _traverse from "@babel/traverse";
const traverse = _traverse.default;
// import traverse from "@babel/traverse";
import _generate from "@babel/generator";
import path from "path";
const generate = _generate.default;
const code = `
    const str="test";
    function hello(){
        str+="- add"; //表达式
        return 'hello world'
    }
`
const srcDir = path.resolve("src/babel")
const fileContent = fs.readFileSync(path.join(srcDir,"./testFunc.js"),{encoding:"utf-8"})
const ast = await parser.parse(code)
traverse(ast,{
    //不常用
    'FunctionDeclaration|VariableDeclaration'(path)  {
        // console.log(path.node)
    },
    // ReturnStatement(path){
    //     console.log(JSON.stringify(path.node))
    // },
    enter(path){
        if(path.isFunctionDeclaration()){
            const methordName = path.node.id.name
            // console.log(JSON.stringify(path.node.body))
        }
        if(path.isVariableDeclaration()){

        }
        if(path.isIdentifier({ name: "str" })){

        }
        if(path.isReturnStatement()){
            if(path.node.argument.value=="hello world"){
                path.node.argument.value = "hello kitty"
            }
        }
    },
    exit(){

    }
})

const result = generate(ast)
// console.log("isReturnStatement:",JSON.stringify(result))
